package com.kfm.jdbc.day0403;

import com.kfm.jdbc.day0328.DBUtils;
import com.kfm.jdbc.day0328.entity.Goods;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class Demo04 {

    public static void main(String[] args) {
        HSSFWorkbook sheets = new HSSFWorkbook();

        HSSFSheet sheet = sheets.createSheet("自定义sheet");

        int rowIndex= 0;
        // 创建行
        HSSFRow row = sheet.createRow(rowIndex ++);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("商品编号");
        row.createCell(1).setCellValue("商品名称");
        row.createCell(2).setCellValue("商品进价");
        row.createCell(3).setCellValue("商品售价");
        row.createCell(4).setCellValue("商品库存");
        row.createCell(5).setCellValue("入库时间");
        row.createCell(6).setCellValue("分类编号");

        // jdbc 查询数据库
//        DBUtils<Goods> dbUtils = new DBUtils<>(1);
//        String sql = "select * from goods";
//        List<Goods> goodsList = dbUtils.select(sql, Goods.class);
//        // 遍历结果集
//        for (Goods goods : goodsList){
//            HSSFRow row1 = sheet.createRow(rowIndex++);
//            row1.createCell(0).setCellValue(goods.getGoodsNo());
//            row1.createCell(1).setCellValue(goods.getGoodsName());
//            row1.createCell(2).setCellValue(goods.getCost());
//            row1.createCell(3).setCellValue(goods.getPrice());
//            row1.createCell(4).setCellValue(goods.getCount());
//            row1.createCell(5).setCellValue(goods.getCreateTime().toLocaleString());
//            row1.createCell(6).setCellValue(goods.getCategoryNo());
//        }

        try (OutputStream fileOut = new FileOutputStream("poi/base.xls")) {
            sheets.write(fileOut);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
